package service

import (
	"fmt"
	"hanyun-admin/src/entity"
	"log"
)

//获取登录用户详情
func SelectUserByUserId(userId int64) entity.User {
	var user entity.User
	_, err := DbEngine.Alias("u").Join("LEFT", []string{"sys_dept", "d"}, "u.dept_id=d.dept_id").Where("u.user_id = ?", userId).Get(&user)
	if err != nil {
		fmt.Println("select user by userId error ", err)
	}
	return user
}

//新增用户信息
func InsertUser(user entity.User) (count int64) {
	count, err := DbEngine.Insert(user)
	if err != nil {
		return
	}
	return
}

//查询用户列表
func SelectUserList(user entity.User) {
	userArr := make([]entity.User, 0)
	sql := DbEngine.Select("*") /*
		if user.Nickname != ""{
			//这是模糊查询的写法
			sql.Where("nickname like ?","%"+user.Nickname+"%")
		}
		if user.Mobile != "" {
			sql.Where("mobile = ?",user.Mobile)
		}*/
	sql.Find(&userArr)
	for i := 0; i < len(userArr); i++ {
		u := userArr[i]
		fmt.Println(u.UserId)
	}
}

//根据用户名密码查询用户
func SelectUserByUserNameAndPassword(userName string) entity.User {
	user := entity.User{}
	_, err := DbEngine.Alias("u").Join("LEFT", []string{"sys_dept", "d"}, "u.dept_id=d.dept_id").Where("u.user_name = ?", userName).Get(&user)
	if err != nil {
		fmt.Println("user login error", err)
	}
	return user
}

//更新用户数据
func UpdateUserData(user entity.User) error {
	_, err := DbEngine.Where("user_id=?", user.UserId).Update(user)
	if err != nil {
		fmt.Println("update user data error ", err)
	}
	return err
}

//用户列表
func SelectSysUserList(user entity.User) []entity.User {
	userList := make([]entity.User, 0)
	sql := DbEngine.Alias("u").Join("LEFT", []string{"sys_dept", "d"}, "u.dept_id=d.dept_id")
	if user.UserName != "" {
		sql.Where("u.user_name like ?", "%"+user.UserName+"%")
	}
	if user.DeptId != 0 {
		sql.Where("(u.dept_id = ? OR u.dept_id IN ( SELECT t.dept_id FROM sys_dept t WHERE FIND_IN_SET (?,ancestors) ))", user.DeptId, user.DeptId)
	}
	if user.Mobile != "" {
		sql.Where("u.mobile like ?", "%"+user.Mobile+"%")
	}
	if user.Status != "" {
		sql.Where("u.status = ?", user.Status)
	}
	err := sql.Select("u.*,d.dept_name").Find(&userList)
	if err != nil {
		log.Println("select user list error ", err)
	}
	return userList
}

//根据条件查询总条数
func SelectSysUserListCount(user entity.User) int {
	sql := DbEngine.Alias("u").Join("LEFT", []string{"sys_dept", "d"}, "u.dept_id=d.dept_id")
	if user.UserName != "" {
		sql.Where("u.user_name like ?", "%"+user.UserName+"%")
	}
	if user.DeptId != 0 {
		sql.Where("(u.dept_id = ? OR u.dept_id IN ( SELECT t.dept_id FROM sys_dept t WHERE FIND_IN_SET (?,ancestors) ))", user.DeptId, user.DeptId)
	}
	if user.Mobile != "" {
		sql.Where("u.mobile like ?", "%"+user.Mobile+"%")
	}
	if user.Status != "" {
		sql.Where("u.status = ?", user.Status)
	}
	count, err := sql.Count(entity.User{})
	if err != nil {
		log.Println("select count form sys_user error ", err)
	}
	return int(count)
}

//查询用户详情
func SelectSysUserInfoByUserId(userId int64) entity.User {
	var user entity.User
	_, err := DbEngine.Alias("u").Join("LEFT", []string{"sys_dept", "d"}, "u.dept_id=d.dept_id").Where("u.user_id=?", userId).Get(&user)
	if err != nil {
		log.Println("select user info by userId error ", err)
	}
	return user
}
